<?php
/**
 * @abstract: Post manager
* @author: HoanVX
* @version $Id: post-manager.php 91 2015-05-14 13:16:33Z voxuanhoan@gmail.com $
*/

/*
 * Template Name: Post manager
*/

// Delte more post
if (isset($_POST['doDelete'])) {
	$postIds = $_POST['postId'];

	foreach ($postIds as $id) {

		wp_delete_post($id);
	}
}

// Delete one post
if (isset($_GET['act']) && isset($_GET['postId']) && strcmp($_GET['act'], "del") == 0 && !empty($_GET['postId'])) {
	wp_delete_post($_GET['postId']);
}


add_style('css/page/post-manager.css');
add_script("js/jquery.validate.js");
add_script('js/page/post-manager.js');
get_header();

?>
<!-- header-section-ends -->
<!-- content-section-starts -->
<div class="content">
	<div class="container">
		<div role="tabpanel">
  			<!-- Nav tabs -->
	  		<ul class="nav nav-tabs" role="tablist">
			    <li role="presentation" class="active"><a href="<?php echo get_the_permalink(172);?>" aria-controls="profile" role="tab" data-toggle="">Profile</a></li>
			    <li role="presentation"><a href="#managerInfo" aria-controls="managerInfo" role="tab" data-toggle="tab">Quản lý thông tin rao</a></li>
			    <li role="presentation"><a href="<?php echo get_the_permalink(177);?>" aria-controls="infoSaved" role="tab" data-toggle="">Danh sách tin đã lưu</a></li>
		  	</ul>

		  	<!-- Tab panes -->
			<div class="tab-content">
				<div role="tabpanel" class="tab-pane" id="profile">
				</div>
			    <div role="tabpanel" class="tab-pane active" id="managerInfo">
					<div class="panel panel-success">
						<!-- Default panel contents -->
						<div class="panel-heading">DANH SÁCH TIN ĐÃ ĐĂNG</div>

						<?php
						// Pagination fix (wp paging ko hoat dong voi custome query
						$tempQuery = $wp_query;
						$wp_query   = NULL;

						// $wp_query: is global object (instance of WP_QUERY()
						$wp_query = search();
						$count = 0;
						?>

						<form action="" method="POST" id="postManageFrm">
							<!-- Table -->
							<table class="table table-hover">
								<thead>
									<tr>
										<td colspan="2"><input type="submit" name="doDelete" value="Xóa tin nhiều tin" class="deletePosts"/></td>
										<td colspan="2"><strong>Tổng số: </strong><span> <?php echo $wp_query->found_posts;?> </span> tin - <strong>Trang </strong><?php $paged = empty($paged) || $paged == 0 ? 1 : $paged; echo $paged ."/" . $wp_query->max_num_pages;?></td>
									</tr>
							  		<tr class="bg-info">
							  			<th class="w2p"><input type="checkbox" name="checkAll" class="checkAll"></th>
										<th class="w2p">STT</th>
										<th class="w20p">Tiêu đề</th>
										<th class="w25p">Nội dung tin</th>
										<th class="w10p">Mục đích đăng</th>
										<th class="w10p">Danh mục đăng</th>
										<th class="w15p">Khu vực đăng</th>
										<th class="w10p">Ngày đăng</th>
										<th class="w5p">Trạng thái</th>
								  	</tr>
								</thead>
								<tbody>
							<?php
								if ($wp_query->have_posts()): while ($wp_query->have_posts()) : $wp_query->the_post();
							?>
								  	<tr>
								  		<td><input type="checkbox" name="postId[]" class="clsCheck" value="<?php echo $post->ID;?>"/></td>
										<th scope="row"><?php echo ++$count;?></th>
										<td><?php echo ucfirst(the_title('','',false));?></td>
										<td><?php the_content();?></td>
										<td><?php echo ChotroiUtils::getValueByKey(CtEnum::$purpose, get_post_meta($post->ID, CT_POST_PURPOSE,true));?></td>
										<td><?php echo get_the_category($post->ID)[0]->cat_name;?></td>
										<td>
										<?php
											$city = ChotroiUtils::getValueByKey(CtEnum::$cities, get_post_meta($post->ID, CT_CITY, true));
											$districtEnum = CityEnum::getDistricts(get_post_meta($post->ID, CT_CITY, true));
											$districtName = ChotroiUtils::getValueByKey($districtEnum, get_post_meta($post->ID, CT_DISTRICT, true));

											echo ucwords($districtName. ' - ' . $city);
										?>
										</td>
										<td><?php echo $post->post_date;?></td>
										<td><?php echo $post->post_status;?>
											<a class="btn btn-success" href="<?php echo get_the_permalink(30). '?act=edit&postId=' . $post->ID;?>">Cập nhập</a>
											<a class="btn btn-success deletePost" href="./?act=del&postId=<?php echo $post->ID;?>">Xóa</a>
										</td>
								  	</tr>
								<?php endwhile;
								?>
								</tbody>
								<tfoot>
									<tr class="bg-info">
							  			<th class="w2p"><input type="checkbox" name="checkAll" class="checkAll"></th>
										<th class="w2p">STT</th>
										<th class="w20p">Tiêu đề</th>
										<th class="w25p">Nội dung tin</th>
									<th class="w10p">Mục đích đăng</th>
									<th class="w10p">Danh mục đăng</th>
									<th class="w15p">Khu vực đăng</th>
									<th class="w10p">Ngày đăng</th>
									<th class="w5p">Trạng thái</th>
							  	</tr>
								<tr>
									<td colspan="9"><input type="submit" name="doDelete" value="Xóa tin nhiều tin"/></td>
								</tr>
							</tfoot>
						</table>
						<!-- pagination here -->
					<?php
							wp_reset_postdata();
						endif;

						if (function_exists("pagination")) {
					    	pagination();
						}

						$wp_query = NULL;
						$wp_query = $tempQuery;
					?>
					</form>
					</div>
			    </div>
			    <div role="tabpanel" class="tab-pane" id="infoSaved">.infoSaved.</div>
		  	</div>
		</div>
	</div>

<!-- Contact's information -->
<?php get_footer("contact");?>
<!-- END Contact's information -->
</div>
<!-- content-section-ends -->
<!-- footer-section-starts -->
<?php get_footer();

/**
 * Search post follow args conditions
 * @param unknown $purpose: the purpose of the post
 * @param unknown $city: the city which post is publish
 * @param unknown $district: the district which post is publish
 * @param unknown $cat: the category of posts
 * @param unknown $subject: subject will
 * @return mixed
 */
function search() {

	// use to Hack pagination trong wp. vi wp custom query thi pagintation doesn't work
	global $paged;

	$args = array(
			"post_status"	=> array("publish", "draft"),
			"paged"			=> $paged,
			"post_type"		=> "post",
// 			"s"				=> $subject, // Search body and title Line 2071 \wp-includes\query.php
// 			"meta_query"	=> array(
// 					array(
// 							'key'     => CT_POST_PURPOSE,
// 							'value'   => $purpose,
// 							'compare' => "="
// 					),
// 			),
	);

// 	if (!empty($city)) {
// 		array_push($args["meta_query"], array(
// 		'key' => CT_CITY,
// 		'value' => $city,
// 		'compare' => '=',

// 		));
// 	}

// 	if (!empty($district)) {
// 		array_push($args["meta_query"], array(
// 		'key' => CT_DISTRICT,
// 		'value' => $district,
// 		'compare' => '=',
// 		));
// 	}

// 	if (!empty($cat)) {
// 		$args["cat"] = $cat;
// 	}

// 	if (!empty($subject)) {
// 		$args["post_title"] = $subject;
// 	}

	return new WP_Query($args);
}
?>